﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Collections; 
using System.Web.UI;
using Liger.Web.Common;

namespace Liger.Web.Common.Extensions
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class TryCatchExtensions
    {  
        /// <summary>
        /// TryCatch 扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="action">要执行的动作</param>
        /// <param name="failureAction">失败的委托</param>
        /// <param name="successAction">成功的委托</param>
        /// <returns>如果成功返回 True</returns>
        public static bool TryCatch<T>(this T source, Action<T> action,
Action<Exception> failureAction, Action<T> successAction) where T : class
        {
            try
            {
                action.Invoke(source);
                successAction.Invoke(source);
                return true;
            }
            catch (Exception ex)
            {
#if DEBUG
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
#endif
                failureAction.Invoke(ex);
                return false;
            }
        }

        /// <summary>
        /// TryCatch 扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="action">要执行的动作，只能是表达式</param>
        /// <param name="failureAction">失败的委托</param>
        /// <returns>如果成功返回 True</returns>
        public static bool TryCatch<T>(this T source, Action<T> action, Action<Exception> failureAction) where T : class
        {
            return source.TryCatch<T>(action, failureAction, c => { });
        }

        /// <summary>
        /// TryCatch 扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="source"></param>
        /// <param name="func">要执行的函数，只能是表达式</param>
        /// <param name="failureAction">失败的委托</param>
        /// <param name="successAction">成功的委托</param>
        /// <returns></returns>
        public static U TryCatch<T, U>(this T source, Func<T, U> func,
            Action<Exception> failureAction, Action<T> successAction) where T : class
        {
            try
            {
                var result = func.Invoke(source);
                successAction.Invoke(source);
                return result;
            }
            catch (Exception ex)
            {
#if DEBUG
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
#endif
                failureAction.Invoke(ex);
                return default(U);
            }
        }

        /// <summary>
        /// TryCatch 扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="source"></param>
        /// <param name="func">要执行的函数，只能是表达式</param>
        /// <param name="failureAction">失败的委托</param>
        /// <returns></returns>
        public static U TryCatch<T, U>(this T source, Func<T, U> func, Action<Exception> failureAction) where T : class
        {
            return source.TryCatch<T, U>(func, failureAction);
        }
         
         

    }
}
